package com.boot.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.boot.web.dao.ProductDao;
import com.boot.web.dao.SkuRelationDao;
import com.boot.web.dao.SkuSelectedDao;
import com.boot.web.dto.SpecTreeDTO;
import com.boot.web.entity.SkuRelation;
import com.boot.web.entity.SkuSelected;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.Product;
import com.boot.web.service.ProductService;

/**
 * 产品控制器
 * @author 【zhouJie】
 * @date 2021-06-02 08:43:22
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private SkuSelectedDao skuSelectedDao;

    @Autowired
    private SkuRelationDao skuRelationDao;

    @Autowired
    private ProductDao productDao;

    @PostMapping("/save")
    public ResponseServer save(Product product) {
        product.setIsShelf("0");
        product.setSales(1);
        product.setUpdateTime(new Date());
        return ResponseServer.success("添加成功", productService.save(product));
    }

    @PutMapping("/update")
    public ResponseServer update(Product product) {
        Product p = productDao.queryById(product.getId());
        if("0".equals(p.getIsShelf())){
            if("1".equals(product.getIsShelf())){
                // 判断是否有SKU
                List<SkuRelation> sku = skuRelationDao.queryByProductId(product.getId());
                if(sku.size() > 0){

                }else{
                    return ResponseServer.error("上架失败，产品SKU不正确");
                }
            }
        }
        product.setUpdateTime(new Date());
        return ResponseServer.success("修改成功", productService.update(product));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        SkuSelected ss = skuSelectedDao.querySkuSelectedByProductId(id);
        if(ss != null){
            skuSelectedDao.delete(ss.getId());
        }
        skuRelationDao.deleteByProductId(id);
        return ResponseServer.success("删除成功", productService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        for(int i = 0; i < ids.length; i++){
            SkuSelected ss = skuSelectedDao.querySkuSelectedByProductId(ids[i]);
            if(ss != null){
                skuSelectedDao.delete(ss.getId());
            }
            skuRelationDao.deleteByProductId(ids[i]);
            productService.delete(ids[i]);
        }
        return ResponseServer.success("批量删除成功");
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(productService.getProductVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(productDao.queryAllVO());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(productService.getById(id));
    }

    @GetMapping("/getSpecAttributeTree/{classifyIdAndProductId}")
    public ResponseServer getSpecAttributeTree(@PathVariable String classifyIdAndProductId) {
        return ResponseServer.success(productService.getSpecAttributeTree(classifyIdAndProductId));
    }

    @PostMapping("/submitSpecAttribute")
    public ResponseServer submitSpecAttribute(SpecTreeDTO specTreeDTO) {
        productService.submitSpecAttribute(specTreeDTO);
        return ResponseServer.success();
    }

}
